﻿using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

//自定义命名空间
using DreamCube.Apps.Common.WinForm.Controls;
using DreamCube.Apps.Common.WinForm.Controls.Progress;
using DreamCube.Framework.DataAccess.Basic;
using DreamCube.Framework.DataAccess.Sqlserver;
using DreamCube.Foundation.Basic.Objects.Log;
using DreamCube.Foundation.Basic.Extensions.System.StringIO;

namespace DC_52Icon.ToolUI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 浏览图标文件夹的按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                this.textBox2.Text = folderBrowserDialog1.SelectedPath;
        }

        ProgressForm progressForm = null;

        /// <summary>
        /// 开始执行数据升级按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (!textBox2.Text.IsDirExit())
            {
                MessageBoxHelper.Information(this, "请选择原来的图标文件目录");
                return;
            }

            DataSet data = GetOldData();
            if (data == null) return;

            ProgressFormStyle style = new ProgressFormStyle()
            {
                BarStep = 1,
                BarMaxLength = data.Tables[0].Rows.Count,
                AutoRunProgressBar = false
            };

            //显示进度条框
            progressForm =
                ProgressBarHelper.ShowSelfProgressDialog(this, new Action(TransData), style);
            progressForm.ShowDialog(this);
        }

        DataSet data;
        private DataSet GetOldData()
        {
            Database oldDB = DbManager.GetDBByConnectionString(textBox1.Text);
            data = oldDB.ExecuteDataSet("select * from [icon]");
            return data;
        }

        private void TransData()
        {
            Database newDB = DbManager.GetDBByConnectionString(textBox3.Text);
            String insertIntoNewDbSql = "insert into ICONIndex([keyword],[zhkeyword],[size12],[icon12],[size16],[icon16]," +
                                         "[size24],[icon24],[size32],[icon32],[size48],[icon48],[size64],[icon64],[size128]," +
                                         "[icon128],[size256],[icon256],[down]) " +
                                         "values('{0}','{1}',{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}," +
                                          "{14},{15},{16},{17},{18})";
            if (data != null && data.Tables.Count > 0)
            {
                DataTable table = data.Tables[0];
                if (table.Rows.Count > 0)
                {
                    for (Int32 i = 0, j = table.Rows.Count; i < j; i++)
                    {
                        String pid = table.Rows[i]["pid"].ToString();
                        newDB.ExecuteNonQuery(CommandType.Text,
                            String.Format(insertIntoNewDbSql, 
                                            table.Rows[i]["keyword"], 
                                            table.Rows[i]["zhkeyword"],
                                          GetImageUnicode(table.Rows[i]["size12"], pid, "12", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon12"], pid, "12", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size16"], pid, "16", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon16"], pid, "16", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size24"], pid, "24", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon24"], pid, "24", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size32"], pid, "32", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon32"], pid, "32", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size48"], pid, "48", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon48"], pid, "48", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size64"], pid, "64", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon64"], pid, "64", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size128"], pid, "128", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon128"], pid, "128", ".ico"),
                                          GetImageUnicode(table.Rows[i]["size256"], pid, "256", ".png"),
                                          GetImageUnicode(table.Rows[i]["icon256"], pid, "256", ".ico"),
                                          table.Rows[i]["down"]));
                        progressForm.PerformStep();
                    }
                }
            }
        }

        //执行保存文件的操作
        private static PicWCFService.PicServiceClient client = new PicWCFService.PicServiceClient();
        private Int64 GetImageUnicode(Object value, String pic, String fileName, String fileType)
        {
            if (value == System.DBNull.Value) return -1;
            Byte[] fileData = null;
            try
            {
                fileData = File.ReadAllBytes(Path.Combine(textBox2.Text, pic) + "\\" + fileName + fileType);
                if (fileData != null)
                    return client.SaveImage(fileData);
            }
            catch (Exception ex)
            {
                LoggerManager.Logger.MakeLogAsync(ex);
            }
            return -1;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = System.Configuration.ConfigurationManager.AppSettings["OldDbConnStr"];
            textBox2.Text = System.Configuration.ConfigurationManager.AppSettings["ICONPath"];
            textBox3.Text = System.Configuration.ConfigurationManager.AppSettings["NewDbConnStr"];
        }
    }
}
